package com.zygh.lz.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zygh.lz.dao.IllegalDataMapper;
import com.zygh.lz.dao.IllegalMapper;
import com.zygh.lz.dao.PoliceDataMapper;
import com.zygh.lz.entity.Equipage;
import com.zygh.lz.entity.Illegal;
import com.zygh.lz.entity.Section;
import com.zygh.lz.service.ServiceStatusService;
import com.zygh.lz.util.DataTime;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: zygh-transform
 * @description: 服务状态检测Impl
 * @author: jhaol
 * @created: 2023/05/19 10:56
 */
@Service
public class ServiceStatusServiceImpl implements ServiceStatusService {
    @Resource
    private EquipageServiceImpl equipageService;

    @Resource
    private PoliceDataMapper policeDataMapper;
    @Resource
    private IllegalMapper illegalMapper;

    @Resource
    private IllegalDataMapper illegalDataMapper;

    /**
     * @return java.lang.Object
     * @author jhoal
     * @description 判断坐标同步服务运行情况
     * @date 11:32 2023/5/19
     **/
    @Override
    public Object coordinateSynchronizationStatus() {
        Date start = DateUtil.parse(DateUtil.date().toString("yyyy-MM-dd") + " 07:00:00");
        Date end = DateUtil.parse(DateUtil.date().toString("yyyy-MM-dd") + " 08:30:00");
        if (DataTime.isEffectiveDate(DateUtil.date(), start, end)) {
            String startTime = DateUtil.offsetMinute(DateUtil.date(), -30).toString();
            String endTime = DateUtil.date().toString();
            int count = equipageService.count(Wrappers.<Equipage>lambdaQuery().between(Equipage::getUploadTime, startTime, endTime));
            if (count > 0) {
                return results(200, "服务正常", 1);
            }
            return results(200, "服务异常", -1);
        }
        return results(200, "服务正常", 1);
    }

    /**
     * @return java.lang.Object
     * @author jhoal
     * @description 判断接处警数据同步服务运行情况
     * @date 11:33 2023/5/19
     **/
    @Override
    public Object alarmWorkSynchronizationStatus() {
        //默认当天
        String startTime = DataTime.getStartTheDay();
        String endTime = DataTime.getEndDate();
        List<Map> policeData = policeDataMapper.findPoliceData(null, startTime, endTime);
        if (policeData.size() > 0) {
            return results(200, "服务正常", 1);
        }
        return results(200, "服务异常", -1);
    }

    /**
     * @return java.lang.Object
     * @author jhoal
     * @description 判断违法数据同步服务运行状态
     * @date 11:33 2023/5/19
     **/
    @Override
    public Object illegalDataSynchronizationStatus() {
        //默认当天
        String startTime = DataTime.getStartTheDay();
        String endTime = DataTime.getEndDate();
        List<Map<String, Object>> illegalData = illegalDataMapper.findIllegalDataByType(startTime, endTime, null, null, 2);
        if (illegalData.size() > 0) {
            return results(200, "服务正常", 1);
        }
        return results(200, "服务异常", -1);
    }


    public Object results(int code, String message, Object data) {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("code", code);
        hashMap.put("message", message);
        hashMap.put("data", data);
        return hashMap;
    }
}
